package mastermind.game;

import java.awt.Component;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTable;

import mastermind.gui.Frame;
import mastermind.socket.protocol;
import mastermind.socket.socketConnect;

public class gameLoop extends Thread{
	
	private Frame frame;
	
	public gameLoop(Frame frame)
	{
		this.frame = frame;
	}
	
	/**
	 * Game loop thread that is the main game loop
	 */
	public void run()
	{
		int response;
		int turnCount = 0;
		JButton button;
		String feedback;
		socketConnect connect = frame.connect(); //receive input and output streams
		DataInputStream dio = connect.getInputStream();
		DataOutputStream dos = connect.getOutputStream();
		String msg = "";
		
		button = frame.getStartGameButton();
		
		System.out.println("GameLoop Thread Started");
		
		//if connect is successful
		if(connect != null && dio != null && dos != null)
		{
			try 
			{
				dos.writeInt(protocol.ATTEMPT_START_GAME);
				System.out.println("Starting Game");
				JTable table = frame.getBoardTable();
				JTable pegtable = frame.getPegTable();
				table.setValueAt("???", 0, 0);
				table.setValueAt("???", 0, 1);
				table.setValueAt("???", 0, 2); //set guess cells
				table.setValueAt("???", 0, 3);
				
				
				while((response = dio.readInt()) != protocol.GAME_ENDED)
				{
					if(response == protocol.TURN_START)
					{
						System.out.println("Turn Start");
						table.setValueAt("???", turnCount, 0);
						table.setValueAt("???", turnCount, 1);
						table.setValueAt("???", turnCount, 2); //set guess next row
						table.setValueAt("???", turnCount, 3);
					}
					if(response == protocol.TURN_END)
					{
						System.out.println("Turn End");
						

					}
					if(response == protocol.GAME_WIN)
					{
						button.setEnabled(true);
						if(turnCount < 4)
						{
							msg = "Congratulations! You won in " + turnCount + " turns!\nYou are a genius.";
						}
						else if(turnCount < 8)
						{
							msg = "Congratulations! You won in " + turnCount + " turns!\nYou are good.";
						}
						else if(turnCount < 10)
						{
							msg = "Congratulations! You won in " + turnCount + " turns!\nYou are not bad.";
						}
						
						JOptionPane.showConfirmDialog((Component) null, msg, "Winner", JOptionPane.DEFAULT_OPTION);
						System.out.println("Game Win");
					}
					if(response == protocol.GAME_LOSE)
					{
						button.setEnabled(true);
						frame.setGameStarted(false); //set game started flag to false when game is over
						button.setEnabled(true);
						msg = "You lose!";
						JOptionPane.showConfirmDialog((Component) null, msg, "Lose", JOptionPane.DEFAULT_OPTION);
						System.out.println("Game Lose");
					}
					if(response == protocol.TURN_COUNT)
					{
						turnCount = dio.readInt();
						System.out.println("Turn Count: " + turnCount);
						frame.setTurnCount(turnCount);

					}
					if(response == protocol.SEND_FEEDBACK)
					{						
						feedback = dio.readUTF();
						String one, two, three, four;
						int feedbackLength = feedback.length(); //calculate how much feedback received
						System.out.println("Feedback: " + feedback);
						int feedbackTurnCount = 0;
						feedbackTurnCount = turnCount;
						feedbackTurnCount--;
						
						switch (feedbackLength) //retrieve and set feedback on client
						{
						case 0:
							//none correct
							break;
						case 1:
							one = feedback.substring(0, 1);
							pegtable.setValueAt(one, feedbackTurnCount, 0);
							break;
						case 2:
							one = feedback.substring(0, 1);
							two = feedback.substring(1, 2);
							pegtable.setValueAt(one, feedbackTurnCount, 0); //set peg cells from server feedback
							pegtable.setValueAt(two, feedbackTurnCount, 1);
							break;
						case 3:
							one = feedback.substring(0, 1);
							two = feedback.substring(1, 2);
							three = feedback.substring(2, 3);
							pegtable.setValueAt(one, feedbackTurnCount, 0);
							pegtable.setValueAt(two, feedbackTurnCount, 1); //set peg cells from server feedback
							pegtable.setValueAt(three, feedbackTurnCount, 2);
							break;
						case 4:
							one = feedback.substring(0, 1);
							two = feedback.substring(1, 2);
							three = feedback.substring(2, 3);
							four = feedback.substring(3, 4);
							pegtable.setValueAt(one, feedbackTurnCount, 0); //set peg cells from server feedback
							pegtable.setValueAt(two, feedbackTurnCount, 1);
							pegtable.setValueAt(three, feedbackTurnCount, 2);
							pegtable.setValueAt(four, feedbackTurnCount, 3);
							break;
						}
					}
				}
			} 
			catch (IOException e) 
			{
				e.printStackTrace();
			}
		}
	}

}
